BetterRegex
Multi-line regex with comments and functions.
Status: Almost ready
There's a test that has a function handler, but I want to provide a default function handler in the library.
Example
For a more thorough example of inputs & outputs, see test/Regexes.php
<?php
$dataSet = [
'one'=> 'one ::ref(two)',
// 'one'=> 'one ::ref(two ;; {{na}}{{cool}}{{happy}})',
'two'=> 'two ::ref(three)',
'three'=> 'three'
];
$startReg = $dataSet['one'];
// $funcHandler = new \RegWithSimpleArray($dataSet);
$br = new \RegWithSimpleArray($dataSet);
$finalReg = $br->parse($startReg);
$targetReg= 'one two three';
// $finalReg == $targetReg
Install
It's not on composer or anything yet...
- git clone
-
require
both the files in thecode
dir.
Regex Functions
Syntax
::funcName(arg1 ;; arg2 ;; {{BabySeal}}{{HappyCat}}{{FunnyGiraffe}})
- Arg list must not include a line break
- Each argument has whitespace trimmed
-
{{value}}{{..}}
is how you declare an array
Function Handler
extend BetterReg
, like:
<?php
class AnimalStuffs extends BetterReg {
public function regFuncName($namespace, $arg1, $arg2, $animals){
$arg1 == 'arg1';
$arg2 == 'arg2';
$animals == ['BabySeal','HappyCat','FunnyGiraffe'];
return $arg1.'-'.$arg2.'-'.$animals[2];
}
}
-
$namespace
comes from(new AnimalStuffs($regexes))->parse($regexes['theFuncOne'], 'namespace')
. This example doesn't utilize the namespace.
Yields
arg1-arg2-FunnyGiraffe